<?php
include_once "xAP-functions/webincludes.php";



$results=dbquery("SELECT SourceID,Location,Alias,SourceType FROM xap_pictures_source order by SourceType,Alias");

foreach($results as $r){
	if ($r["SourceType"]==0){
		$src[$r["SourceID"]]["lcn"]=$r["Location"];
		$src[$r["SourceID"]]["name"]=$r["Alias"];
		$src[$r["SourceID"]]["type"]=$r["SourceType"];
	}
	if ($r["SourceType"]==1){
		$cctv[$r["SourceID"]]["lcn"]=$r["Location"];
		$cctv[$r["SourceID"]]["name"]=$r["Alias"];
		$cctv[$r["SourceID"]]["type"]=$r["SourceType"];
	}
}


// list all
$results=dbquery("SELECT distinct xap_pictures_item.SourceID,xap_pictures_item.FullFolder  FROM xap_pictures_item
inner join xap_pictures_source on xap_pictures_item.sourceid=xap_pictures_source.sourceid 
where xap_pictures_source.sourcetype=0
order by FullFolder;");

foreach($results as $r){

	$tmp=str_replace($src[$r["SourceID"]]["lcn"],"",$r["FullFolder"]);
	$tmp=str_replace("\\","/",$tmp);
	if($tmp[0]=="/"){$tmp=substr($tmp,1);}
	
	$item=explode("/",$tmp);
	
	//echo count($item).$tmp."<BR>";

	if($_GET["root"]=="source"){
		//if(isset($src[$r["SourceID"]]["fld"][$r["SourceID"]."-".urlencode($item[0])])==false){
			if(count($item)<2){
					$children="false";				
				}else{
					$children="true";
				}
			$src[$r["SourceID"]]["fld"][$r["SourceID"]."---".urlencode($item[0])]=array("c"=>$children,
														"name"=>$item[0]);
		//}
		}else{
			// need to fin items of this level
			$base=$_GET["root"];
			$blastitems=explode("---",$base);
			$children='false';
			//build match for item
			$lstitems=array();
			for($i=0;$i<(count($blastitems));$i++){
				$lstitems[]=$item[$i];
				$displayname=$item[$i];
			}
			$lst=implode("---",$lstitems);
			$lst=urlencode($lst);

			
			//only show items of this level
			if(substr($r["SourceID"]."---".$lst,0,strlen($base."---"))==$base."---"){
				if (isset($srcitem[$r["SourceID"]."---".$lst])==false){
				$srcitem[$r["SourceID"]."---".$lst]=array("c"=>1,
														"name"=>$displayname);	
						//echo "-".$r["FullFolder"]."         ".$lst."---<BR>";
						if(strpos("-".$r["FullFolder"],$lst."---")){
							$srcitem[$r["SourceID"]."---".$lst]["c"]++;
						}
					}else{
				$srcitem[$r["SourceID"]."---".$lst]["c"]++;
					}
			}
		}
		//print_r($src);
}

if($_GET["root"]=="source"){
?>
[
{'text': "<a href='#' OnClick='LoadLatest()'>Latest</a>",'classes': 'boldtext', 'expanded':'true'},
<?if(isset($src)):?>
<?foreach($src as $skey=>$sv):?>
{'text': "<div OnClick='LoadFolder(\"<?=$skey?>\")'><?=$sv["name"]?></div>",'classes': 'boldtext', 'children':[
<?if(isset($sv["fld"])):?>
<?foreach($sv["fld"] as $vkey=>$v):?>
{'text': "<div OnClick='LoadFolder(\"<?=$vkey?>\")'><?=$v["name"]?></div>",'classes': 'normaltext','id': "<?=$vkey?>", "hasChildren": <?=$v["c"]?>},
<? endforeach;?>
<?endif;?>
]},
<? endforeach;?>
<?endif;?>
<?if(isset($cctv)):?>
{'text': "CCTV",'classes': 'boldtext', 'expanded' : true, 'children':[

<?foreach($cctv as $vkey=>$v):?>
{'text': "<div OnClick='LoadCCTV(\"<?=$vkey?>\")'><?=$v["name"]?></div>",'classes': 'normaltext'},
<? endforeach;?>

]}
<?endif;?>
]
<?php
}else{
?>
	[
	<? if(isset($srcitem)):?>
		<?foreach($srcitem as $skey=>$sv):?>
		{'text': "<div OnClick='LoadFolder(\"<?=$skey?>\")'><?=$sv["name"]?></div>",'classes': 'normaltext','id': "<?=$skey?>", "hasChildren": <? if($sv["c"]==0){ echo "false";}else{ echo "true";} ?>},
		<? endforeach;?>
	<? endif;?>
	]
<?php	
}
